package com.gobo.jinghuimall.product.service.impl;

import com.gobo.jinghuimall.product.dao.AttrAttrgroupRelationDao;
import com.gobo.jinghuimall.product.dao.AttrDao;
import com.gobo.jinghuimall.product.entity.AttrAttrgroupRelationEntity;
import com.gobo.jinghuimall.product.entity.AttrEntity;
import com.gobo.jinghuimall.product.service.AttrAttrgroupRelationService;
import com.gobo.jinghuimall.product.service.CategoryService;
import com.gobo.jinghuimall.product.vo.AttrGroupRelationVo;
import com.gobo.jinghuimall.product.vo.AttrGroupWithAttrsVo;
import com.gobo.jinghuimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gobo.common.utils.PageUtils;
import com.gobo.common.utils.Query;

import com.gobo.jinghuimall.product.dao.AttrGroupDao;
import com.gobo.jinghuimall.product.entity.AttrGroupEntity;
import com.gobo.jinghuimall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationDao attrgroupRelationDao;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatelogId(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        IPage<AttrGroupEntity> page = null;
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId != 0L) {
            wrapper.eq("catelog_id", catelogId);
        }
        page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getByIdAndCatelogPath(Long attrGroupId) {
        AttrGroupEntity byId = this.getById(attrGroupId);
        Long catelogId = byId.getCatelogId();
        List<Long> catelogPath = categoryService.getCatelogPath(catelogId);
        byId.setCatelogPath(catelogPath);
        return byId;
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrgroupId) {
        //获取属性分组关联表中所有的属性ID后查询
        List<AttrAttrgroupRelationEntity> attr_group_id = attrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<Long> longList = attr_group_id.stream().map((obj) -> {
            return obj.getAttrId();
        }).collect(Collectors.toList());
        if (longList.size() == 0 || longList == null) {
            return null;
        }
        List<AttrEntity> attrEntityList = attrDao.selectBatchIds(longList);
        return attrEntityList;
    }

    @Override
    @Transactional
    public void deleteAttrRelation(AttrGroupRelationVo[] attrGroupRelationVo) {
        List<AttrAttrgroupRelationEntity> collect = Arrays.asList(attrGroupRelationVo).stream().map(obj -> {
            AttrAttrgroupRelationEntity attrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(obj, attrgroupRelationEntity);
            return attrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrgroupRelationDao.deleteAttrRelation(collect);
    }

    @Override
    public void saveAttrRelation(List<AttrGroupRelationVo> attrGroupRelationVo) {
        List<AttrAttrgroupRelationEntity> collect = attrGroupRelationVo.stream().map(
                obj -> {
                    AttrAttrgroupRelationEntity attrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                    BeanUtils.copyProperties(obj, attrgroupRelationEntity);
                    return attrgroupRelationEntity;
                }
        ).collect(Collectors.toList());
        attrAttrgroupRelationService.saveBatch(collect);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(obj -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(obj, attrGroupWithAttrsVo);
            attrGroupWithAttrsVo.setAttrs(this.getAttrRelation(obj.getAttrGroupId()));
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return vos;

    }


}